package com.ketangabc.sdk.Utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.usb.UsbConfiguration;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static android.content.Context.USB_SERVICE;
/**
 * @author gaoxin 19-1-24 上午11:05
 * @version V1.0.0
 * @name DetectingAudioHardware
 * @mail godfeer@aliyun.com
 * @description  检测音频硬件
 */
public class DetectingAudioHardware {

    private static String TAG = "DetectingAudioHardware";
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static String detectUsbAudioDevice(Context context) {
        HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) context.getSystemService(USB_SERVICE)).getDeviceList();
        for (Map.Entry entry : deviceHashMap.entrySet()) {
            UsbDevice device = (UsbDevice) entry.getValue();
            if (null != device) {
                for (int i = 0; i < device.getConfigurationCount(); i++) {
                    UsbConfiguration configuration = device.getConfiguration(i);
                    for (int j = 0; j < configuration.getInterfaceCount(); j++) {
                        UsbInterface usbInterface = configuration.getInterface(j);
                        if (UsbConstants.USB_CLASS_AUDIO == usbInterface.getInterfaceClass()) {
                            return Objects.requireNonNull(detectInputDeviceWithShell()).toString();
                        }
                    }
                }
            }
        }
        return "没有USB音频设备";
    }


    public static StringBuffer detectInputDeviceWithShell() {
        try {
            //获得外接USB输入设备的信息 ///proc/asound/cards    /proc/bus/input/devices
            Process p = Runtime.getRuntime().exec("cat /proc/bus/input/devices");
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            StringBuffer stringBuffer = null;
            stringBuffer = new StringBuffer();
            while ((line = in.readLine()) != null) {
                String deviceInfo = line.trim();
                //对获取的每行的设备信息进行过滤，获得自己想要的。
//                if (deviceInfo.contains("Name=")){
//                    Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo);
//                    stringBuffer.append(deviceInfo);
//                }
                stringBuffer.append(deviceInfo).append("\n");
            }
            if (stringBuffer.length() <=0 ){
                stringBuffer.append("暂无外设");
                return stringBuffer;
            }
            Log.d(TAG, "-----------------------");
            return stringBuffer;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
